package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.entity.Article;
import com.hoongs.v1.entity.Comment;
import com.hoongs.v1.mapper.CommentMapper;
import com.hoongs.v1.service.CommentService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

@Service
public class CommentServiceImpl implements CommentService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该标签！";

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Result save(Comment comment) {
        if (StringUtils.isNotBlank(comment.getId())) {
            this.update(comment);
        } else {
            this.add(comment);
        }
        return null;
    }

    @Override
    public Result add(Comment comment) {
        if (commentMapper.insert(comment) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findCommentById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (commentMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Comment comment) {
        if (!findCommentById(comment.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (commentMapper.updateById(comment) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Comment comment = commentMapper.selectById(id);
        if (comment == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(comment);
    }

    @Override
    public Result selectList(String article_id, String status) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<Comment>();
        if (StringUtils.isNotBlank(article_id)) {
            wrapper.like("article_id", article_id);
        }
        if (StringUtils.isNotBlank(status)) {
            wrapper.eq("status", status);
        }
        Collection list = commentMapper.selectList(wrapper);
        return Result.success(list);
    }

    @Override
    public Result selectPageList(Integer page, Integer size) {
        if (!checkPageQuery(page, size)) return Result.error("分页参数错误");
        Page<Comment> daoPage = new Page<>(page, size);
        IPage<Comment> commentDaoIPage = commentMapper.selectPage(daoPage, null);
        PResult rs = new PResult(commentDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询用户是否存在
     */
    public Boolean findCommentById(String id) {
        Comment comment = commentMapper.selectById(id);
        return comment != null;
    }

    /**
     * 检查分页参数是否为空
     */
    public Boolean checkPageQuery(Integer page, Integer size) {
        return page != null && page > 0 && size != null && size > 0;
    }

}
